const gulp = require('gulp')
const ts = require('gulp-typescript')
const tsProject = ts.createProject('tsconfig.json')
const rimraf = require('rimraf')

gulp.task('clean', () => {
  rimraf.sync('dist')
  rimraf.sync('generators')
  return Promise.resolve()
})

gulp.task(
  'build',
  gulp.series(['clean'], () => {
    return tsProject.src().pipe(tsProject()).js.pipe(gulp.dest('dist'))
  })
)

gulp.task(
  'copy:generators',
  gulp.series(['build'], () => {
    return gulp.src('dist/**/*').pipe(gulp.dest('./'))
  })
)

gulp.task(
  'copy:templates',
  gulp.series(['copy:generators'], () => {
    return gulp.src('src/generators/app/templates/**/*').pipe(gulp.dest('./generators/app/templates'))
  })
)

gulp.task(
  'default',
  gulp.series(['copy:templates'], () => {
    rimraf.sync('dist')
    rimraf.sync('types')
    return Promise.resolve()
  })
)
